<?php

require_once dirname(__FILE__).'/cf/config.php';
require_once cf\Config::path.'param.php';
require_once cf\Config::path.'user.php';
require_once cf\Config::path.'image.php';
require_once cf\Config::path.'session.php';
require_once cf\Config::path.'api/admin.php';
require_once cf\Config::path.'api/page.php';
require_once cf\Config::path.'api/shop.php';
require_once cf\Config::path.'api/user.php';

define('SMARTY_DIR','./smarty/');
require_once SMARTY_DIR . 'Smarty.class.php';

$smarty = new Smarty;
$smarty->compile_check = true;
$smarty->debugging = false;
$smarty->addPluginsDir(cf\Config::path.'components/');

date_default_timezone_set('UTC');
setlocale(LC_ALL,"en_US.utf8");

cf\session::open();

if (array_key_exists('log',$_POST) && array_key_exists('pwd',$_POST)) {
	try {
		cf\User::login(cf\param::get('log'), cf\param::get('pwd'), cf\param::exist('remember'));
	} catch(Exception $e) {
	}
	if (!cf\User::getLoggedIn()) {
		$smarty->assign('show_error','Не удалось авторизироваться. Пожалуйста, проверьте правильность логина и пароля.');
	}
	Header('Location: /stylist');
}

$user = cf\User::getLoggedIn();
$smarty->assign('user',$user);


$smarty->assign('topMenu',cf\api\page\getTree('topMenu',false,1));

$clientId = 0;
$clients = array();
$isStylist = 0;
$stylistId = 0;
if ($user) {
	if (!$user->is_a('stylist') && !$user->is_a('client') ) {
		cf\api\user\logout();
		Header('Location: /');
		exit;
	}
	if ($user->is_a('stylist')) {
		$isStylist = $user->is_a('stylist') ? 1 : 0;
		$stylistId = $user->id();
		$clients = cf\api\admin\getList('stylist_clients',0,0,array(),null,array('stylist_id'=>$user->id()));
		$clientId = $clients[0][1];
		if (cf\session::has('clientID')) {
			$clientId = cf\session::get('clientID');
		}
	} else {
		$clientId = $user->id();
		$stylistId = cf\query2var('SELECT stylist_id FROM client_stylist WHERE client_id=:id',array('id'=>$user->id()));
	}
}
else if ($_SERVER['REQUEST_URI'] != '/') {
	Header('Location: /');
	exit;
}
$smarty->assign('clientId', $clientId);
$smarty->assign('clients', json_encode($clients));
$smarty->assign('isStylist', $isStylist);
$smarty->assign('stylistId', $stylistId);

$currTopPage = 0;
$smarty->assignByRef('currTopPage',$currTopPage);

function show($template, $name, $crumbs=array(), $title='', $keywords='', $descr='')
{
	global $smarty, $lang;

	$smarty->assign('name',$name);
	$smarty->assign('crumbs',$crumbs);
	$smarty->assign('title',$title);
	$smarty->assign('keywords',$keywords);
	$smarty->assign('descr',$descr);
	
	$smarty->assign('tpl', "$template.tpl");

	$smarty->display('main.tpl');
}

$smarty->assign('groups', json_encode(cf\api\admin\getList('categories')));
$smarty->assign('brands', json_encode(cf\api\admin\getList('manufacturers',0,0,array('name'=>'a'))));
$smarty->assign('seasons',json_encode(cf\api\admin\getList('seasons')));
$smarty->assign('closets',json_encode(cf\api\admin\getList('closets',0,0,array(),null,array('owner_id'=>$clientId))));
?>